<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" size="mini" label-width="80px">
      <el-form-item size="small" label="上级部门" prop="parentName">
        <el-popover
          ref="departListPopover"
          placement="bottom-start"
          trigger="click">
          <sys-depart-tree @select-change="departTreeSelectChange"></sys-depart-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:departListPopover :readonly="true" placeholder="部门"></el-input>
      </el-form-item>
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="部门名称"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import SysDepartTree from './sysDepartTree'
  export default {
    components: {
      SysDepartTree
    },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: '',
          parentName: '',
          name: '',
          orderNum: '',
          createUserId: '',
          createdBy: '',
          createdOn: '',
          modifiedUserId: '',
          modifiedBy: '',
          modifiedOn: ''
        },
        dataRule: {
          parentName: [
            { required: true, message: '上级部门不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdepartment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.sysDepartment.parentId
                this.dataForm.parentName = data.sysDepartment.parentName
                this.dataForm.name = data.sysDepartment.name
                this.dataForm.orderNum = data.sysDepartment.orderNum
                this.dataForm.createUserId = data.sysDepartment.createUserId
                this.dataForm.createdBy = data.sysDepartment.createdBy
                this.dataForm.createdOn = data.sysDepartment.createdOn
                this.dataForm.modifiedUserId = data.sysDepartment.modifiedUserId
                this.dataForm.modifiedBy = data.sysDepartment.modifiedBy
                this.dataForm.modifiedOn = data.sysDepartment.modifiedOn
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdepartment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'parentId': this.dataForm.parentId,
                'parentName': this.dataForm.parentName,
                'name': this.dataForm.name,
                'orderNum': this.dataForm.orderNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      departTreeSelectChange (data, node) {
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.label
      }
    }
  }
</script>
